<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="gv-page-container" fxLayout="column">
  <h1 *ngIf="!isEmpty">Users</h1>

  <div *ngIf="!isEmpty" class="users-search" fxLayout="column">
    <mat-form-field id="users-search-input" appearance="fill" floatLabel="always" style="max-width: 90%;">
      <mat-icon matPrefix>search</mat-icon>
      <mat-label>Search for users</mat-label>
      <input [(ngModel)]="searchValue" type="text" matInput placeholder="Search" (keyup.enter)="onSearch()">
      <button *ngIf="searchValue" mat-icon-button matSuffix (click)="discardSearchValue()"><mat-icon>close</mat-icon></button>
      <button *ngIf="!organizationContext" mat-icon-button matSuffix (click)="openAdvancedSearch();"><mat-icon>tune</mat-icon></button>
      <mat-hint align="end">Press <b>enter</b> to perform the search request <mat-icon (click)="openDialog()" style="font-size: 15px; color:black; cursor: pointer;">info_outline</mat-icon></mat-hint>
    </mat-form-field>
    <mat-card *ngIf="displayAdvancedSearchMode" class="advanced-search-card">
      <mat-card-content>
        <div fxLayout="row" style="align-items: center;">
          <label fxFlex="25">Identity provider</label>
          <mat-form-field>
            <mat-select name="idp" [(ngModel)]="selectedIdPs" multiple>
              <mat-option *ngFor="let idp of identityProviders" [value]="idp.id">
                {{idp.name}}
              </mat-option>
            </mat-select>
          </mat-form-field>
        </div>
        <div fxLayout="row" style="align-items: center;">
          <label fxFlex="25">Application</label>
          <mat-form-field>
            <mat-select name="application" [(ngModel)]="selectedApplications" multiple>
              <mat-option *ngFor="let application of applications" [value]="application.id">
                {{application.name}}
              </mat-option>
            </mat-select>
          </mat-form-field>
        </div>
        <div fxLayout="row" style="align-items: center; margin-top: 10px;">
          <div fxFlex="5">
            <mat-checkbox (change)="toggleDisabledUsers($event)"></mat-checkbox>
          </div>
          <label>Select disabled users</label>
        </div>
      </mat-card-content>
      <mat-card-actions fxLayoutAlign="end end">
        <button mat-button (click)="closeAdvancedSearch();">CLOSE</button>
        <button mat-raised-button color="primary" (click)="onAdvancedSearch();">SEARCH</button>
      </mat-card-actions>
    </mat-card>
  </div>

  <div class="users-content" *ngIf="!isEmpty">
    <ngx-datatable class="material"
                   [columnMode]="'flex'"
                   [headerHeight]="40"
                   [footerHeight]="40"
                   [rowHeight]="50"
                   [rows]='users'
                   [externalPaging]="true"
                   [count]="page.totalElements"
                   [offset]="page.pageNumber"
                   [limit]="page.size"
                   (page)='setPage($event)'>
      <ngx-datatable-column name="Name" [flexGrow]="4" [cellClass]="'user-name-cell'">
        <ng-template let-row="row" ngx-datatable-cell-template>
          <div class="user-header" fxLayout="row">
            <div class="user-avatar"
                 matBadge="&#8288;"
                 matBadgeSize="small"
                 [ngClass]="(row.enabled && !accountLocked(row)) ? 'user-enabled': (!row.enabled ? 'user-disabled' : accountLocked(row) ? 'user-locked' : '')"
                 [matTooltip]="(row.enabled && !accountLocked(row)) ? 'User enabled': (!row.enabled ? 'User disabled' : accountLocked(row) ? 'User locked' : '')">
              <app-user-avatar [user]="row" [width]="'40'"></app-user-avatar>
            </div>
            <div class="user-information" fxLayout="column">
              <a [routerLink]="[row.id]" *ngIf="hasReadPermissions()">{{row.displayName}}</a>
              <span *ngIf="!hasReadPermissions()">{{row.displayName}}</span>
              <small>{{row.username}}</small>
            </div>
          </div>
        </ng-template>
      </ngx-datatable-column>
      <ngx-datatable-column name="Identity Provider" [flexGrow]="2" *hasPermission="[requiredReadPermission]">
        <ng-template let-row="row" ngx-datatable-cell-template>
          {{row.source}}
        </ng-template>
      </ngx-datatable-column>
      <ngx-datatable-column name="Last login" [flexGrow]="2" *hasPermission="[requiredReadPermission]">
        <ng-template let-row="row" ngx-datatable-cell-template>
          <span *ngIf="(row.loggedAt)">{{row.loggedAt | humanDate}}</span>
          <span *ngIf="!(row.loggedAt)">Never logged in</span>
        </ng-template>
      </ngx-datatable-column>
      <ngx-datatable-column name="Actions" [flexGrow]="1" *hasPermission="[requiredReadPermission]">
        <ng-template let-row="row" ngx-datatable-cell-template>
          <div fxLayout="row" class="gv-table-cell-actions">
            <a mat-icon-button [routerLink]="[row.id]"><mat-icon matTooltip="More info">more_horiz</mat-icon></a>
          </div>
        </ng-template>
      </ngx-datatable-column>
    </ngx-datatable>
  </div>

  <app-emptystate *ngIf="isEmpty"
                  [message]="'Users will appear here'"
                  [subMessage]="'No data to display'"
                  [icon]="'person'"></app-emptystate>

  <div *ngIf="createMode" [ngClass]="{'gv-add-button': !isEmpty, 'gv-add-button-center': isEmpty}">
    <a mat-fab color="primary" [routerLink]="['new']">
      <mat-icon>add</mat-icon>
    </a>
  </div>
</div>
